package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ware.annotation.Token;
import com.example.ware.model.po.*;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.model.vo.FireWorksVo;
import com.example.ware.model.vo.PlaceVO;
import com.example.ware.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/place")
@CrossOrigin
public class PlaceController {

    @Autowired
    private PlaceService placeService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private AccessService accessService;

    @Autowired
    private PinfoService pinfoService;

    @Resource
    RedisTemplate redisTemplate;


    //    添加新的分区
    @Token
    @PostMapping("/init")
    public ResponseData addPlace(String wid, @RequestBody Place place){
        place.setNowstorage(0.0);
        place.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        place.setWid(wid);

        boolean i= placeService.save(place);

        if(i){
            return new ResponseData(i,true,"创建成功",200);
        }else{
            return new ResponseData(i,true,"创建失败",201);

        }
    }


//    根据仓库id获取所有的分区
    @GetMapping("/warePlace")
    public ResponseData getWarePlace(String  wid){
        QueryWrapper<Place> qw=new QueryWrapper<>();
        qw.eq("wid",wid);
        List<Place> placeList=placeService.list(qw);

        return new ResponseData(placeList,true,"获取成功",200);
    }

//  获取每个分区所有的烟花数据
    @Token
    @GetMapping("/allInfo")
    public ResponseData allInfo(@RequestHeader String token){
        Admin admin = (Admin)redisTemplate.opsForValue().get("Admin_" + token);
        String wid;
        if(admin.getAuth()==1){
            return new ResponseData(null,false,"你不是仓库专员",201);
        }else{
            wid=admin.getWid();
        }



        QueryWrapper<Place> qw=new QueryWrapper<>();
        qw.eq("wid",wid);
        List<Place> placeList = placeService.list(qw);
      List<Pinfo> pinfoList=new ArrayList<>();
        for(Place p:placeList){
            QueryWrapper<Pinfo> rr=new QueryWrapper<>();
            rr.eq("pid",p.getId());
            List<Pinfo> list = pinfoService.list(rr);
            pinfoList.addAll(list);
        }

        if(pinfoList!=null){
            return new ResponseData(pinfoList,true,"获取成功",200);
        }else{
            return new ResponseData(null,false,"获取失败",201);
        }

    }

//    删除
@Token
@DeleteMapping("/del")
public ResponseData del(String pid ){
    Place place = placeService.getById(pid);
    if (place.getNowstorage()>0){
        return new ResponseData(null,false,"该区域已经投入使用，存在数据无法删除",500);
    }else{
        boolean b = placeService.removeById(pid);
        if(b){
            return new ResponseData(null,true,"删除成功",200);

        }else{
            return new ResponseData(null,false,"删除失败",201);
        }
    }
}
}
